<?php

declare (strict_types=1);

namespace App\Models\GameDB\Pet;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PoolUser extends Model
{
    protected $connection = 'mysql_game';
    protected $table = 'lottery_pet_pool_user';
    //protected $fillable = [];
    protected $guarded = [];

    //protected $casts = ['id' => 'int', 'user_id' => 'integer', 'amount' => 'integer', 'balance' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function one(array $filters)
    {
        return self::query()->where($filters)->first();
    }

    function updateOrCreateBalance($userId, $balance)
    {
        //todo db raw是否有效确认
        self::query()->updateOrCreate(['user_id' => $userId], ['user_id' => $userId, 'balance' => DB::raw('balance +'. $balance)]);
    }

    function updateData($filters, $data)
    {
        //todo db raw是否有效确认
        return self::query()->where($filters)->update($data);
    }

    function firstOrCreate($userId)
    {
        return self::query()->firstOrCreate(['user_id' => $userId], ['user_id' => $userId, 'level' => 1, 'exp' => 0, 'balance' => 0]);
    }
}